﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;

namespace WOWMultiPlay
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Helper_Other.CreateRunningProfileTempFile();

            this.Opacity = Setting_Global.AppOpacity / 100;
            this.Text = Setting_Global.Profile.ProfileName;

            this.Width = Setting_Global.WindowWidth;
            this.Height = Setting_Global.WindowHeight;

            CreateProfileButtons(Setting_Global.Profile.Buttons);
        }

        Button[] ProfileWinFormButtons;

        private void CreateProfileButtons(Helper_Profile.Profile.Button[] ProfileButtons)
        {
            CreateButtonsLayoutPanel(ProfileButtons);

            ProfileWinFormButtons = new Button[ProfileButtons.Length];

            int AddedButtonCount = 0;
            int AddingCol = 0;
            int AddingRow = 0;

            for (int i = 0; i < ProfileButtons.Length; i++)
            {
                ProfileWinFormButtons[i] = new Button();
                ProfileWinFormButtons[i].Dock = DockStyle.Fill;
                ProfileWinFormButtons[i].Text = ProfileButtons[i].ButtonName;
                ProfileWinFormButtons[i].Tag = ProfileButtons[i];
                ProfileWinFormButtons[i].Click += new System.EventHandler(this.ProfileWinFormButton_Click);
                ProfileWinFormButtons[i].UseVisualStyleBackColor = true;

                ButtonsLayoutPanel.Controls.Add(this.ProfileWinFormButtons[i], AddingCol, AddingRow);
                AddedButtonCount++;
                AddingCol++;

                if (AddingCol > ButtonsLayoutPanel.ColumnCount - 1)
                {
                    AddingCol = 0;
                    AddingRow++;
                }
            }
        }

        private void CreateButtonsLayoutPanel(Helper_Profile.Profile.Button[] ProfileButtons)
        {
            int ButtonsLayoutPanelLines = ProfileButtons.Length % Setting_Global.MaxButtonCountPerLine == 0
                           ? ProfileButtons.Length / Setting_Global.MaxButtonCountPerLine
                           : ProfileButtons.Length / Setting_Global.MaxButtonCountPerLine + 1;

            if (ProfileButtons.Length < Setting_Global.MaxButtonCountPerLine)
            {
                ButtonsLayoutPanel.ColumnCount = ProfileButtons.Length;
                ButtonsLayoutPanel.RowCount = 1;
            }
            else
            {
                ButtonsLayoutPanel.ColumnCount = Setting_Global.MaxButtonCountPerLine;
                ButtonsLayoutPanel.RowCount = ButtonsLayoutPanelLines;
            }

            ButtonsLayoutPanel.ColumnStyles.Clear();
            Single ColPercent = 100 / ButtonsLayoutPanel.ColumnCount;

            for (int i = 0; i < ButtonsLayoutPanel.ColumnCount; i++)
            {
                ButtonsLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, ColPercent));
            }

            ButtonsLayoutPanel.RowStyles.Clear();
            Single RowPercent = 100 / ButtonsLayoutPanel.RowCount;

            for (int i = 0; i < ButtonsLayoutPanel.RowCount; i++)
            {
                ButtonsLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, RowPercent));
            }
        }

        private void ProfileWinFormButton_Click(object sender, EventArgs e)
        {
            Button ProfileWinFormButton = (Button)sender;
            Helper_Profile.Profile.Button ProfileButton = (Helper_Profile.Profile.Button)ProfileWinFormButton.Tag;

            if (ProfileButton.SendKeysThread == null)
            {
                ProfileWinFormButton.ForeColor = Setting_Global.RunningButtonFontColor;
                Helper_Keyboard.SendKeys_NewThread(Setting_Global.Instance, ProfileWinFormButton);
            }
            else
            {
                ProfileButton.SendKeysThread.Abort();

                ProfileWinFormButton.ForeColor = Setting_Global.NormalButtonFontColor;
                ProfileButton.SendKeysThread = null;
            }
        }

        private void MainForm_ResizeEnd(object sender, EventArgs e)
        {
            Setting_Global.WindowWidth = this.Width;
            Setting_Global.WindowHeight = this.Height;
            Helper_Settings.SaveWindowsSize(Setting_Global.SettingsFilePath);
        }
    }
}